<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %><html lang="en">
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>AdminLTE 3 | Registration Page</title>

  <!-- Google Font: Source Sans Pro -->
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,400i,700&display=fallback">
  <!-- Font Awesome -->
  <link rel="stylesheet" href="plugins/fontawesome-free/css/all.min.css">
  <!-- icheck bootstrap -->
  <link rel="stylesheet" href="plugins/icheck-bootstrap/icheck-bootstrap.min.css">
  <!-- Theme style -->
  <link rel="stylesheet" href="dist/css/adminlte.min.css">


</head>
<body class="hold-transition register-page">
<div class="register-box">
  <div class="register-logo">
    <a href="index2.html"><b>Admin</b>LTE</a>
  </div>

  <div class="card">
    <div class="card-body register-card-body">
      <p class="login-box-msg">Register a new membership</p>

      <form action="registerchk" method="post">
        <div class="input-group mb-3">
          <input id="username" type="text" class="form-control" placeholder="请输入用户名" name="username"
                 onKeyUp="checkName()"
          >
          <div class="input-group-append">
            <div class="input-group-text">
              <span class="fas fa-user" ></span>
            </div>
          </div>
        </div>
        <div>
          <span id="usernameInfo"></span>
        </div>
        <div class="input-group mb-3">
          <input type="password" class="form-control" placeholder="请输入密码" name="password">
          <div class="input-group-append">
            <div class="input-group-text">
              <span class="fas fa-lock"></span>
            </div>
          </div>
        </div>
        <div class="input-group mb-3">
          <input type="password" class="form-control" placeholder="请输入重复密码">
          <div class="input-group-append">
            <div class="input-group-text">
              <span class="fas fa-lock"></span>
            </div>
          </div>
        </div>
        <div class="row">
          <div class="col-8">
            <div class="icheck-primary">
              <input type="checkbox" id="agreeTerms" name="terms" value="agree">
              <label for="agreeTerms">
                I agree to the <a href="#">terms</a>
              </label>
            </div>
          </div>
          <!-- /.col -->
          <div class="col-4">
            <button type="submit" class="btn btn-primary btn-block">Register</button>
          </div>
          <!-- /.col -->
        </div>
      </form>



      <a href="login.jsp" class="text-center">I already have a membership</a>
    </div>
    <!-- /.form-box -->
  </div><!-- /.card -->
</div>
<!-- /.register-box -->

<!-- jQuery -->
<script src="plugins/jquery/jquery.min.js"></script>
<!-- Bootstrap 4 -->
<script src="plugins/bootstrap/js/bootstrap.bundle.min.js"></script>
<!-- AdminLTE App -->
<script src="dist/js/adminlte.min.js"></script>


<script>

  // jQuery方式
  function checkName()
  {
    // 当前界面使用的是bootstrap，使用了jQuery
    // $ jQuery的操作符
    // $("#username")  通过css选择器 找到 界面元素对象
    // $.ajax     jQuery ajax函数
    // { url: 请求地址,   resultType:返回数据的格式, success: 返回数据成功后的处理 }

    var username=$("#username").val();
    var url = "checkname?username="+username;
    console.log(url);
    $.ajax({
      url:url,
      resultType:"json",
      success:function (data)
      {
        console.log(data);
        console.log(data.result);
        if(data.result==false)
        {
          console.log("不可用!")
          $("#usernameInfo").html("用户名不可用");
        }
        else
        {
          $("#usernameInfo").html();
        }
      }
    });

  }

  // 普通DOM方法

  // ajax对象
  // var xmlHttpReq;

  // function checkName()
  // {
  //   // 1. 获取用户填写用户名
  //   var username = document.getElementById("username").value;
  //
  //   // 2. 创建ajax对象
  //   xmlHttpReq = new XMLHttpRequest();
  //
  //   // 3. 设定处理函数
  //   xmlHttpReq.onreadystatechange = handle;
  //
  //
  //   // 4. 发送请求
  //   var url = "checkname?username="+username;
  //   xmlHttpReq.open("get",url,true); // true 表示 异步 false 同步
  //   xmlHttpReq.send(null);
  //
  // }
  //
  // function handle()
  // {
  //   if(xmlHttpReq.readyState == 4 && xmlHttpReq.status == 200)
  //   {
  //     console.log(xmlHttpReq.responseText);
  //
  //     var usernameInfo = document.getElementById("usernameInfo");
  //     console.log(usernameInfo);
  //
  //     var data = JSON.parse(xmlHttpReq.responseText);
  //     if(data.result==false)
  //     {
  //       usernameInfo.innerHTML = "用户名不可用"
  //     }
  //     else
  //     {
  //       usernameInfo.innerHTML = "";
  //     }
  //
  //
  //   }
  // }
</script>
</body>
</html>